package _15并发工具类_CyclicBarrier;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class GameTask implements Runnable{
    static CyclicBarrier barrier = new CyclicBarrier(5);//需要等5个玩家完成

    @Override
    public void run() {
        String name = Thread.currentThread().getName();

        Random r = new Random();
        int time = r.nextInt(5)+1;
        try {
            Thread.sleep(time*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(name+"完成加载"+time);

        try {
            barrier.await();//main主线程要等5个玩家加载完才能开始游戏
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

        System.out.println("所有玩家加载完成，开始游戏");
    }
}
